﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.TraCuu_Update
{
    public partial class BanSachGUI : UserControl, GUI.UctrlGUI
    {
        BLL.DanhSachHoaDonBanSachBLL _DanhSachHoaDonBLL;
        BLL.DanhSachSachBLL _DanhSachSachBLL;
        BLL.DanhSachKhachHangBLL _DanhSachKhachHangBLL;

        class cbxItem
        {
            public int _ID;
            public String _Text;
            public int _IDKhachHang;

            public cbxItem(int id, int idkhachhang, String text)
            {
                _IDKhachHang = idkhachhang;
                _ID = id;
                _Text = text;
            }

            public override string ToString()
            {
                return _Text;
            }
        }

        public BanSachGUI()
        {
            InitializeComponent();
            _DanhSachHoaDonBLL = new BLL.DanhSachHoaDonBanSachBLL();
            _DanhSachSachBLL = new BLL.DanhSachSachBLL();
            _DanhSachKhachHangBLL = new BLL.DanhSachKhachHangBLL();
        }

        public String getName()
        {
            return "TRA CỨU HÓA ĐƠN BÁN SÁCH";
        }

        //private void btn_Tim_Click(object sender, EventArgs e)
        //{
        //    UpdateCB();
        //    if (cbx_MaPhieu.Items.Count > 0)
        //        cbx_MaPhieu.SelectedIndex = 0;
        //    else
        //    {
        //        ResetGui();
        //        gv_List.Rows.Clear();
        //    }
        //}

        private void cbx_MaPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateGV();
            UpdateKH();
        }

        private void BanSachGUI_Load(object sender, EventArgs e)
        {
            UpdateCB();
            if (cbx_MaPhieu.Items.Count > 0)
                cbx_MaPhieu.SelectedIndex = 0;
            else
            {
                ResetGui();
                gv_List.Rows.Clear();
            }
        }

        private void UpdateCB()
        {
            cbx_MaPhieu.Items.Clear();
            List<DTO.HoaDonBanSachDTO> lis =
                _DanhSachHoaDonBLL.GetHoaDonByNgayLap(dt_NgayNhap.Value);
            foreach (DTO.HoaDonBanSachDTO dto in lis)
            {
                cbx_MaPhieu.Items.Add(new cbxItem(dto.ID, 
                    dto.IDKhachHang,
                    "Hóa Đơn " + dto.ID.ToString()));
            }
        }

        private void UpdateGV()
        {
            gv_List.Rows.Clear();
            List<DTO.ChiTietBanSachDTO> lis =
                _DanhSachHoaDonBLL.GetChiTietByHoaDon(
                (cbx_MaPhieu.SelectedItem as cbxItem)._ID);

            foreach (DTO.ChiTietBanSachDTO dto in lis)
            {
                DTO.DauSachDTO s = _DanhSachSachBLL.GetSachByID(dto.IDSach);
                String[] item = new String[] { 
                    dto.IDSach.ToString(),
                    (gv_List.RowCount+1).ToString(),
                    s.Ten,
                    s.TheLoai,
                    dto.SoLuong.ToString(),
                    dto.DonGia.ToString()
                };
                gv_List.Rows.Add(item);
            }
        }

        private void UpdateKH()
        {
            DTO.KhachHangDTO dto = _DanhSachKhachHangBLL.GetKhachHangByID(
                (cbx_MaPhieu.SelectedItem as cbxItem)._IDKhachHang);
            txt_DiaChi.Text = dto.DiaChi;
            txt_DienThoai.Text = dto.DienThoai;
            txt_Email.Text = dto.Email;
            txt_HoTen.Text = dto.HoTen;
        }

        private void ResetGui()
        {
            txt_DiaChi.Text = String.Empty;
            txt_DienThoai.Text = String.Empty;
            txt_Email.Text = String.Empty;
            txt_HoTen.Text = String.Empty;
        }

        private void dt_NgayNhap_ValueChanged(object sender, EventArgs e)
        {
            UpdateCB();
            if (cbx_MaPhieu.Items.Count > 0)
                cbx_MaPhieu.SelectedIndex = 0;
            else
            {
                ResetGui();
                gv_List.Rows.Clear();
            }
        }
    }
}
